package com.vsd.service.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.demo.domain.Authority;
import com.demo.domain.UserCreateForm;
import com.demo.repository.AuthorityRepository;
import com.demo.util.DateUtil;
import com.demo.util.SqlUtils;
import com.demo.util.StringUtil;
import com.mdx.mobile.commons.verfy.Md5;
import com.vsd.domain.User;
import com.vsd.repository.UserRepository;
import com.vsd.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(UserServiceImpl.class);
	@Autowired
	private  UserRepository userRepository;
	@Autowired
	private  AuthorityRepository authorityRepository;

	@Autowired
	private Environment env;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 查找用户名
	 */
	@Override
	public int countByCreateTime(Date d) {
		return userRepository.countByCreateTime(DateUtil.strToDate(DateUtil.dateToStr(d,"yyyy-MM-dd")+" 00:00:00", "yyyy-MM-dd HH:mm:ss"));
	}


	/**
	 * 查找用户名
	 */
	@Override
	public User getUserByUsername(String username) {
		LOGGER.debug("Getting user by username={}", username);
		return userRepository.findByUsername(username);
	}
 
	 

	@Override
	public Object listAllUsers(Pageable p) {
		Page<User> users = userRepository.findAll(p);
		return users;
	}

	@Override
	public Object getUserList(int current, int rowCount, String searchPhrase) {
//		HashMap<String, Object> ret = new HashMap<String, Object>();
//		ArrayList<Object> pList = new ArrayList<Object>();
//		Long total = userRepository.count();
//		int i = 0;
//
//		if (rowCount == -1) {
//			rowCount = new Long(total).intValue();
//		}
		Page<User> users = userRepository.findByUsernameContaining(
				searchPhrase, new PageRequest(current - 1, rowCount));
//		for (User user : users) {
//			HashMap<String, Object> data = new HashMap<String, Object>();
//			data.put("id", user.getId());
//			data.put("username", user.getUsername());
//			data.put("image", user.getImage());
//			if (user.getEnabled() == 1) {
//				data.put("enabled", "<font color='green'>启用</font>");
//			} else {
//				data.put("enabled", "<font color='red'>禁用</font>");
//			}
//
//			List<Authority> authorities = authorityRepository
//					.findByUsername(user.getUsername());
//			ArrayList<String> arrayList = new ArrayList<String>();
//			for (Authority authority : authorities) {
//				if (authority.getAuthority().equals("ROLE_ADMIN")) {
//					arrayList.add("管理员");
//				} else if (authority.getAuthority().equals("ROLE_USER")) {
//					arrayList.add("用户");
//				}
//			}
//			data.put("role", arrayList.toString());
//			pList.add(data);
//			i++;
//		}
//
//		ret.put("current", 1);
//		ret.put("rowCount", i);
//		ret.put("rows", pList);
//		ret.put("total", total);

		return users;
	}

	@Override
	public Page<User> findByPhone(String phone, Pageable p) {
		// TODO Auto-generated method stub
		return userRepository.findByPhone(phone,p);
	}

	@Override
	public int countByAccount(String account) {
		// TODO Auto-generated method stub
		return userRepository.countByUserName(account);
	}

	@Override
	public User save(User user) {
		// TODO Auto-generated method stub
		user.setId(StringUtil.getUUID());
		return userRepository.save(user);
	}

	@Override
	public User getUserByUsernameAndPassword(String username, String password) {
		// TODO Auto-generated method stub
		return userRepository.findByPasswordAndUsername(password, username);
	}

	@Override
	public void updatePassword(String username, String password) {
		// TODO Auto-generated method stub
		String sql = "update f_user set password = "+password +" where username = "+username;
		jdbcTemplate.equals(sql);
	}

	@Override
	public User findByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return userRepository.findOne(id);
	}


	@Override
	public Page<User> findAll(Pageable p) {
		// TODO Auto-generated method stub
		return userRepository.findAll(p);
	}


	@Override
	public int updateByPrimaryKey(User u) {
		try {
			String sql = SqlUtils.update(u, true, " where id = '"+u.getId()+"'");
			return jdbcTemplate.update(sql);
		} catch (Exception e) {
			return 0;
		}
	}
	
	
}
